package com.example.a16478082;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;

public class MenuActivity extends AppCompatActivity {
    CheckBox cb_xcr, cb_hs, cb_dj, cb_szy, cb_djt, cb_xbc;
    RadioGroup rg_cheap;
    Button btn_submit;
    EditText et_useCheap;
    double totalPrice = 0;
    Food xcr,hs,dj,szy,djt,xbc;
    boolean isCheap;
    Card c1,c2;
    double cheapLeavel;
//    ArrayList<Card> cardList=new Array<Card>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        initView();
        initEvent();
        initData();
    }

    private void initEvent() {

        rg_cheap.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_useCheap:
                        et_useCheap.setVisibility(View.VISIBLE);
                        et_useCheap.setFocusable(true);
                        et_useCheap.setFocusableInTouchMode(true);
                        et_useCheap.requestFocus();
                        break;
                    case R.id.rb_unUseCheap:
                        et_useCheap.setVisibility(View.GONE);
                        break;
                }
            }
        });

        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (cb_xcr.isChecked()){
                    totalPrice+=xcr.getFoodPrice();

                }

                if(cb_hs.isChecked()){
                    totalPrice+=hs.getFoodPrice();
                }

                if(cb_dj.isChecked()){
                    totalPrice+=dj.getFoodPrice();
                }

                if(cb_szy.isChecked()){
                    totalPrice+=szy.getFoodPrice();
                }

                if(cb_djt.isChecked()){
                    totalPrice+=djt.getFoodPrice();
                }

                if(cb_xbc.isChecked()){
                    totalPrice+=xbc.getFoodPrice();
                }

//                Toast.makeText(getApplicationContext(),totalPrice+"",Toast.LENGTH_SHORT).show();
                String cheapCard=et_useCheap.getText().toString();
                if(cheapCard.equals(c1.getCheapNumber().toString())){
                   cheapLeavel=c1.getCheapLeaver();
                   isCheap=true;
                }
                if (cheapCard.equals(c2.getCheapNumber().toString())){
                    cheapLeavel=c2.getCheapLeaver();
                    isCheap=true;
                }
                if(isCheap){
                    totalPrice=totalPrice*cheapLeavel;

                }

                Toast.makeText(getApplicationContext(),totalPrice+"",Toast.LENGTH_SHORT).show();
                totalPrice=0;
                isCheap=false;

                Intent intent=new Intent();
                intent.setClass(MenuActivity.this,OrderActivity.class);
                MenuActivity.this.startActivity(intent);
            }
        });
    }

    private void initData(){
        xcr=new Food("小炒肉",30.0);
        hs=new Food("红烧豆腐",8.0);
        dj=new Food("蛋饺",10.0);
        szy=new Food("水煮鱼",50.0);
        djt=new Food("大鸡腿",20.0);
        xbc=new Food("小白菜",6.0);

        cb_xcr.setText(xcr.getFoodName()+"("+xcr.getFoodPrice()+")RMB");
        cb_hs.setText(hs.getFoodName()+"("+hs.getFoodPrice()+")RMB");
        cb_dj.setText(dj.getFoodName()+"("+dj.getFoodPrice()+")RMB");
        cb_szy.setText(szy.getFoodName()+"("+szy.getFoodPrice()+")RMB");
        cb_djt.setText(djt.getFoodName()+"("+djt.getFoodPrice()+")RMB");
        cb_xbc.setText(xbc.getFoodName()+"("+xbc.getFoodPrice()+")RMB");

        c1=new Card(88888888,0.8);
        c2=new Card(66666666,0.6);
    }
    private void initView() {
        cb_xcr = findViewById(R.id.cb_xcr);
        cb_hs = findViewById(R.id.cb_hs);
        cb_dj = findViewById(R.id.cb_dj);
        cb_szy = findViewById(R.id.cb_szy);
        cb_djt = findViewById(R.id.cb_djt);
        cb_xbc = findViewById(R.id.cb_xbc);
        btn_submit = findViewById(R.id.btn_submit);
        rg_cheap = findViewById(R.id.rg_cheap);
        et_useCheap = findViewById(R.id.et_useCheap);



    }
}